<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="5" :xs="22" >
        <div class="head-container">
          <el-input
            v-model="robot_name"
            placeholder="请输入机器人名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px" @change="getRobotName($event)"/>
        </div>
        <div class="head-container" style="border:1px solid #0a328a;">
          <!--机器人列表-->
          <el-table v-loading="false" :data="robotList1" @row-click="getSelectdata" @selection-change="handleSelectionChange" :cell-style="{borderColor:'rgb(1 5 43)'}" :header-cell-style="{borderColor: 'rgb(1 5 43)'}">
            <el-table-column label="编号" align="center" prop="robot_num"/>
            <el-table-column label="名称" align="center" prop="robot_name"  width="90px"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    @click="sureupdate(scope.row)"
                  >确认更新</el-button>
              </template>
            </el-table-column>
          </el-table>
          <!-- <table style="margin: auto;width: 100%;color: #FFFFFF;text-align:center">
            <tr style="background: #00A9F8;opacity: 0.4;line-height: 30px;font-size: 13px;">
              <td>编号</td>
              <td>名称</td>
            </tr>
            <tbody v-for="(robot,index) in robotList1">
            <tr @click="getSelectdata(robot)" style="border-bottom:solid 1px #084272;cursor:pointer;background: #0f2b62;line-height:36px;font-size: 13px;font-size: 13px;color: rgb(147, 164, 191);">
              <td v-text="robot.robot_num"></td>
              <td>{{robot.robot_name}}</td>
            </tr>
            </tbody>
          </table>-->
        </div>
      </el-col>
<!-- 查询条件区域-->
  <el-col :span="18" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-="width:80px">
      <el-form-item label="机器人名称" prop="robot_name">
        <el-input
          v-model="queryParams.robot_name"
          placeholder="请输入机器人名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
      <el-form-item label="文件名称" prop="file_name">
        <el-input
          v-model="queryParams.file_name"
          placeholder="请输入文件名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
<!--      <el-form-item label="用户名称" prop="user_name">-->
<!--        <el-input-->
<!--          v-model="queryParams.user_name"-->
<!--          placeholder="请输入用户名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--          size="small"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
     <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['robot:updatepros:add']"
        >上传更新程序</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['robot:updatepros:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--  数据列表区域-->
    <el-table v-loading="loading" :data="robotproList" @selection-change="handleSelectionChange" :cell-style="{borderColor:'rgb(1 5 43)'}" :header-cell-style="{borderColor: 'rgb(1 5 43)'}">
      <el-table-column type="selection" align="center" />
      <el-table-column label="机器人名称" align="center" prop="robot_name"  width="90px"/>
      <el-table-column label="文件名称" align="center" prop="file_name"  width="200px"/>
      <el-table-column label="上传用户" align="center" prop="user_name" width="100px"/>
      <el-table-column label="备注" align="center" prop="remark"  width="400px"/>
      <el-table-column label="创建时间" align="center" prop="update_time" width="200px">
        <template slot-scope="scope">
          <span>{{
              parseTime(scope.row.update_time)
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['robot:updatepros:remove']"
          >删除</el-button>
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="sureupdate(scope.row)"
            v-hasPermi="['robot:updatepros:update']"
          >确认</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!--列表分页区域-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);"
    />
  </el-col>
    </el-row>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" @close="handleCancle" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="机器人" prop="robot_id">
              <el-select v-model="form.robot_id" size="small"  placeholder="请选机器人名称" clearable>
                <el-option
                  v-for="item in robotList"
                  :key="item.id"
                  :label="item.robot_name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-upload style="padding-left:12%;padding-top: 0.4%"
               class="upload-demo"
               ref="upload"
               action=""
               accept=".bin"
               :limit="1"
               :file-list="fileList"
               :before-upload="beforeUpload1">
              <el-button slot="trigger" size="small" type="primary">选取上传BIN文件</el-button>
              <div slot="tip" class="el-upload__tip" style="color:red">注意！先断开机器人连接！！上传bin文件</div>
              <div slot="tip" class="el-upload-list__item-name">{{fileName}}</div>
            </el-upload>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer" style="padding-top: 40px">
        <el-button type="primary" @click="submitForm1">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {listonPageRobotConfig, listRobotConfig} from "@/api/robot/robotconfig/robotconfig";
import {delRobotPro, listRobotPro, sendFileBin, updateRobotPro} from "@/api/robot/updatepros/updatepro";
import "@/assets/styles/robt-bg.css";

export default {
  data() {
    return {
      fileName: '',
      localFilePath:'',
      file_name:'',
      robot_name: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      robot_names:[],
      fileList:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      robotList: [],
      robotList1:[],
      robotproList:[],
      // 弹出层标题
      title: "",
      //是否显示弹出层
      open: false,
      //查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        robot_id: [
          { required: true, message: "机器人名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getrobotList();
    this.getrobotList1();
    setInterval(()=>{
      this.getList(); //刷新列表
    },30000);//30秒
  },
  methods: {
    //上传附件
    beforeUpload1(file){
      this.fileName = null;
      this.files = file;
      const extension = file.name.split('.')[1] === 'bin'
      const isLt2M = file.size / 1024 / 1024 < 20
      if (!extension) {
        this.fileName = null;
        this.files = null;
        this.$message.warning('上传只能是bin格式!')
        return false // 返回false不会自动上传
      }
      if (!isLt2M) {
        this.fileName = null;
        this.files = null;
        this.$message.warning('上传大小不能超过4MB!')
        return false // 返回false不会自动上传
      }
      this.fileName = file.name;
      return false // 返回false不会自动上传
    },
    //自定义对话框关闭方法
    handleCancle () {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    /** 查询机器人配置列表 */
    getList() {
      this.loading = true;
      listRobotPro(this.queryParams).then(response => {
        this.robotproList = response.rows;
        this.total = response.total;
        this.loading = false
      });
    },
    getrobotList1() {
      this.loading = false;
      listonPageRobotConfig(this.queryParams).then(response => {
        this.robotList1 = response.rows;
        this.loading = false;
      });
    } ,
    getrobotList(){
      this.loading = true;
      listRobotConfig(this.queryParams).then(response => {
        this.robotList = response.rows;
        this.total = response.total;
        this.loading = false
      });
    },
    /** 根据输入框机器人名称，查询机器人列表 */
    getRobotName(robot_name){
      this.queryParams.robot_name = robot_name;
      this.getrobotList1();
    },
    getSelectdata(row){
      this.queryParams.robot_id = row.id;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.fileName = undefined;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.robot_id = undefined;
      this.queryParams.robot_name = undefined;
      this.queryParams.file_name = undefined;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.robot_names = selection.map(item => item.robot_name)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
      this.title = "更新程序";
    },
    /** 提交按钮 */
    submitForm1: function() {
      if(this.fileName == null || this.fileName == ""){
          this.$message.warning('请选择要上传的文件！')
          return false
        }
        if(typeof this.form.robot_id =="undefined"||this.form.robot_id == ""){
          this.$message.warning('机器人id地址不为空！')
          return false
        }
        const formData = new FormData();
        formData.append('file',this.files);//filename是键，file是值，就是要传的文件，test.zip是要传的文件名
        // 实体参数
        formData.append("robot_id",this.form.robot_id);
        sendFileBin(formData).then(response => {
           this.$modal.msgSuccess(response.msg);
           this.getList();
        });
        this.cancel();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const filename =row.file_name || this.file_name
      this.$modal.confirm('是否确认删除程序文件"' + filename + '"的数据项？').then(function() {
        return delRobotPro(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    sureupdate(row) {
      const robot_name = row.robot_name || this.robot_name
      this.$modal.confirm('请先断开 "' + robot_name + '" 连接！再发送更新指令').then(function() {
        const formData1 = new FormData();
        formData1.append('robot_id',row.id);
        updateRobotPro(formData1).then((response) => {
          this.$modal.msgSuccess(response.msg);
        }).catch(() => {});
      });
    }
  }
};
</script>
